package com.sz.zhiling.controller;


import com.sz.zhiling.model.PageBean;
import com.sz.zhiling.model.Result;
import com.sz.zhiling.model.TCity;
import com.sz.zhiling.service.ICityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller//注入服务
@RequestMapping("/citys")
public class CityController {

    @Autowired
    private ICityService iCityService;

    //分页查询所有城市
    @RequestMapping("/getAllByPages")

    public String getAllCity(Model model,@RequestParam(required = false,defaultValue = "1") int index){

         model.addAttribute("pb",iCityService.getAllCity(index,5));
         model.addAttribute("plist",iCityService.getAllProvinces());
         return "city/list";
    }




    //条件查询 @RequestBody ：把前端传入的json: {"cId":"0","cName":"州","fId":19}  转为tcity
    @RequestMapping("/searchAll")
    public String searchAll( TCity tCity,Model model){
        model.addAttribute("pb",iCityService.searchAllCity(1,5,tCity));
        model.addAttribute("plist",iCityService.getAllProvinces());
        return  "city/list";
    }

    //删除
    @RequestMapping("/delete")
    public String deleteCity(int cId){
        iCityService.deleteCity(cId);
        return "redirect:/citys/getAllByPages?index=1";
    }



}
